﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;

namespace CommModule.Security
{
    public class AssimEncryption
    {
        public static String Encrypt(String toEncrypt, RSACryptoServiceProvider encryptor)
        {

            byte[] toEncryptBytes = Encoding.UTF8.GetBytes(toEncrypt);

            byte[] cipheredBytes = encryptor.Encrypt(toEncryptBytes, false);

            return Convert.ToBase64String(cipheredBytes);            
        }

        public static String Decrypt(String toDecrypt, RSACryptoServiceProvider decryptor)
        {
            byte[] toDecryptBytes = Convert.FromBase64String(toDecrypt);

            byte[] decipheredBytes = decryptor.Decrypt(toDecryptBytes, false);

            return Encoding.UTF8.GetString(decipheredBytes);
        }

        public static String SignMessage(String toSign, RSACryptoServiceProvider signer)
        {
            byte[] toSignBytes = Encoding.UTF8.GetBytes(toSign);

            byte[] signedBytes = signer.SignData(toSignBytes, "SHA1");

            return Convert.ToBase64String(signedBytes);
        }

        public static bool ValidateSignature(string originalMessage, string cipheredSignature, RSACryptoServiceProvider decryptor)
        {
            byte[] signedMessageBytes = Convert.FromBase64String(cipheredSignature);
            byte[] originalMessageBytes = Encoding.UTF8.GetBytes(originalMessage);

            return decryptor.VerifyData(originalMessageBytes, "SHA1", signedMessageBytes);

           
        }
    }
}
